#include "Show.h"
#include "ui_Show.h"

#include <QMutex>

QMutex g_show_rect_mutex;

Show::Show(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Show)
{
    ui->setupUi(this);
}

Show::~Show()
{
    delete ui;
}

void Show::play(QString strFileName)
{
    VideoCtrl::getInstance()->StartPlay(strFileName, ui->label->winId());
    // VideoCtl::GetInstance()->StartPlay(strFileName, ui->label->winId());
}

void Show::onFrameDimensionChanged(int nFrameWidth, int nFrameHeight)
{
    m_nLastFrameWidth = nFrameWidth;
    m_nLastFrameHeight = nFrameHeight;

    this->changeShow();
}

void Show::changeShow()
{
    g_show_rect_mutex.lock();

    if (m_nLastFrameWidth == 0 && m_nLastFrameHeight == 0)
    {
        ui->label->setGeometry(0, 0, width(), height());
    }
    else
    {
        float aspect_ratio;
        int width, height, x, y;
        int scr_width = this->width();
        int scr_height = this->height();

        aspect_ratio = (float)m_nLastFrameWidth / (float)m_nLastFrameHeight;

        height = scr_height;
        width = lrint(height * aspect_ratio) & ~1;
        if (width > scr_width)
        {
            width = scr_width;
            height = lrint(width / aspect_ratio) & ~1;
        }
        x = (scr_width - width) / 2;
        y = (scr_height - height) / 2;


        ui->label->setGeometry(x, y, width, height);
    }

    g_show_rect_mutex.unlock();
}
